/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.liwalo.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.liwalo.dao.CiudadDAO;
import com.liwalo.dao.ProvinciaDAO;
import com.liwalo.dao.SubcategoriaDAO;
import com.liwalo.entities.Categoria;
import com.liwalo.entities.Pais;
import com.liwalo.entities.Provincia;
import com.opensymphony.xwork2.ActionSupport;

/**
 *
 * @author JR
 */	
public class JSONData extends ActionSupport{
  
	private static final long serialVersionUID = 1L;
	
	private static final Log log = LogFactory.getLog(JSONData.class);
	
	private List<? extends Object> data;	
	private List<String> p;
    
    public String execute() throws Exception{    	
        return SUCCESS;
    }
    
    public String provincias() throws Exception{
    	
    	data = new ArrayList<Object>();
    	
    	try {
    		ProvinciaDAO provinciaDAO = new ProvinciaDAO();
        	data = provinciaDAO.getAllByPais(new Pais(Integer.valueOf(p.get(0))));	
		} catch (Exception e) {
			log.error("Error obteniendo la lista de provincias", e);
		}
    	
        return SUCCESS;
    }
    
    public String ciudades() throws Exception{
    	
    	data = new ArrayList<Object>();
    	
    	try {
    		CiudadDAO ciudadDAO = new CiudadDAO();
        	data = ciudadDAO.getAllByProvincia(new Provincia(Integer.valueOf(p.get(0))));	
		} catch (Exception e) {
			log.error("Error obteniendo la lista de ciudades", e);
		}
    	
        return SUCCESS;
    }
    
    public String subcategorias() throws Exception{
    	
    	data = new ArrayList<Object>();
    	
    	try {
    		SubcategoriaDAO subcategoriaDAO = new SubcategoriaDAO();
        	data = subcategoriaDAO.getAllByCategoria(new Categoria(Integer.valueOf(p.get(0))));
		} catch (Exception e) {
			log.error("Error obteniendo la lista de subcategorias", e);
		}
    	
        return SUCCESS;
    }

	public void setP(List<String> p) {
		this.p = p;
	}

	public List<? extends Object> getData() {
		return data;
	}

	public void setData(List<? extends Object> data) {
		this.data = data;
	}
    
	
    
}
